<template>

  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="交易日期" prop="dealDate">
        <el-date-picker clearable
                        v-model="queryParams.dealDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择统计日期(交易日期)">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="zcList(1)"
          v-hasPermi="['account:day:list']"
        >收入查询</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="zcList(2)"
          v-hasPermi="['account:day:list']"
        >支出查询</el-button>
      </el-col>

<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['account:day:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['account:day:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['account:day:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['account:day:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    </el-row>

    <el-table v-loading="loading" :data="dayList"
              :summary-method="getSummaries" show-summary
              @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="商户id" align="center" prop="businessId" />
      <el-table-column label="商户名称" align="center" prop="businessName" />
      <el-table-column label="海链只有线上，暂时无用 " align="center" prop="onlineFlg" />-->
      <el-table-column label="交易日期" align="center" prop="dealDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dealDate, '{y}-{m}-{d}') }}</span>
        </template>

      </el-table-column>
<!--      <el-table-column label="操作类型,1：收入，2：支出" align="center" prop="operationType" />-->
      <el-table-column label="总计" align="center" prop="total" />
      <el-table-column label="押金" align="center" prop="deposit" />
      <el-table-column label="租金" align="center" prop="rent" />
      <el-table-column label="电费" align="center" prop="electricityFee" />
      <el-table-column label="水费" align="center" prop="waterFee" />
      <el-table-column label="宽带费" align="center" prop="netFee" />
      <el-table-column label="燃气费" align="center" prop="gasFee" />
      <el-table-column label="有线电视费" align="center" prop="cableFee" />
      <el-table-column label="其它" align="center" prop="czFee" />
<!--      <el-table-column label="其他" align="center" prop="otherFee" />
      <el-table-column label="预留字段1" align="center" prop="reserve1" />
      <el-table-column label="预留字段2" align="center" prop="reserve2" />
      <el-table-column label="预留字段3" align="center" prop="reserve3" />
      <el-table-column label="预留字段4" align="center" prop="reserve4" />
      <el-table-column label="预留字段5" align="center" prop="reserve5" />
      <el-table-column label="删除标识：0:未删除，1：删除" align="center" prop="delFlg" />
      <el-table-column label="创建用户ID" align="center" prop="createUserid" />
      <el-table-column label="更新用户ID" align="center" prop="updateUserid" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['account:day:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['account:day:remove']"
          >删除</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改海链财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户id" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入商户id" />
        </el-form-item>
        <el-form-item label="商户名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="海链只有线上，暂时无用 " prop="onlineFlg">
          <el-input v-model="form.onlineFlg" placeholder="请输入海链只有线上，暂时无用 " />
        </el-form-item>
        <el-form-item label="统计日期(交易日期)" prop="dealDate">
          <el-date-picker clearable
            v-model="form.dealDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择统计日期(交易日期)">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总计" prop="total">
          <el-input v-model="form.total" placeholder="请输入总计" />
        </el-form-item>
        <el-form-item label="押金" prop="deposit">
          <el-input v-model="form.deposit" placeholder="请输入押金" />
        </el-form-item>
        <el-form-item label="租金" prop="rent">
          <el-input v-model="form.rent" placeholder="请输入租金" />
        </el-form-item>
        <el-form-item label="电费" prop="electricityFee">
          <el-input v-model="form.electricityFee" placeholder="请输入电费" />
        </el-form-item>
        <el-form-item label="水费" prop="waterFee">
          <el-input v-model="form.waterFee" placeholder="请输入水费" />
        </el-form-item>
        <el-form-item label="宽带费" prop="netFee">
          <el-input v-model="form.netFee" placeholder="请输入宽带费" />
        </el-form-item>
        <el-form-item label="燃气费" prop="gasFee">
          <el-input v-model="form.gasFee" placeholder="请输入燃气费" />
        </el-form-item>
        <el-form-item label="有线电视费" prop="cableFee">
          <el-input v-model="form.cableFee" placeholder="请输入有线电视费" />
        </el-form-item>
        <el-form-item label="商户充值金额(在海链财务报表中算作收入的一部分）" prop="czFee">
          <el-input v-model="form.czFee" placeholder="请输入商户充值金额(在海链财务报表中算作收入的一部分）" />
        </el-form-item>
        <el-form-item label="其他" prop="otherFee">
          <el-input v-model="form.otherFee" placeholder="请输入其他" />
        </el-form-item>
        <el-form-item label="预留字段1" prop="reserve1">
          <el-input v-model="form.reserve1" placeholder="请输入预留字段1" />
        </el-form-item>
        <el-form-item label="预留字段2" prop="reserve2">
          <el-input v-model="form.reserve2" placeholder="请输入预留字段2" />
        </el-form-item>
        <el-form-item label="预留字段3" prop="reserve3">
          <el-input v-model="form.reserve3" placeholder="请输入预留字段3" />
        </el-form-item>
        <el-form-item label="预留字段4" prop="reserve4">
          <el-input v-model="form.reserve4" placeholder="请输入预留字段4" />
        </el-form-item>
        <el-form-item label="预留字段5" prop="reserve5">
          <el-input v-model="form.reserve5" placeholder="请输入预留字段5" />
        </el-form-item>
        <el-form-item label="删除标识：0:未删除，1：删除" prop="delFlg">
          <el-input v-model="form.delFlg" placeholder="请输入删除标识：0:未删除，1：删除" />
        </el-form-item>
        <el-form-item label="创建用户ID" prop="createUserid">
          <el-input v-model="form.createUserid" placeholder="请输入创建用户ID" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>



<script>
import { listDay, getDay, delDay, addDay, updateDay } from "@/api/account/day";

export default {
  name: "Day",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 海链财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)表格数据
      dayList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessId: null,
        businessName: null,
        onlineFlg: null,
        dealDate: null,
        operationType: null,
        total: null,
        deposit: null,
        rent: null,
        electricityFee: null,
        waterFee: null,
        netFee: null,
        gasFee: null,
        cableFee: null,
        czFee: null,
        otherFee: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        }
          // index >= 3 || index <= 11 是你需要计算的列
          // 当然也可以指定一个或者多个列
        // index === 2 || index === 3 这样值求和第二个与第三个列
        else if (index >= 2 && index <= 9) {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                // 保存了两位小数点
                return Math.floor((prev + curr) * 100) / 100;
              } else {
                // 保存了两位小数点
                return Math.floor(prev * 100) / 100;
              }
            }, 0)
          } else {
            sums[index] = 'N/A'
          }
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    /** 查询海链财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)列表 */
    getList() {
      this.loading = true;
      listDay(this.queryParams).then(response => {
        this.dayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessId: null,
        businessName: null,
        onlineFlg: null,
        dealDate: null,
        operationType: null,
        total: null,
        deposit: null,
        rent: null,
        electricityFee: null,
        waterFee: null,
        netFee: null,
        gasFee: null,
        cableFee: null,
        czFee: null,
        otherFee: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null


      };
      this.resetForm("form");
    },
    /** 收入支出查询 */
    zcList(operationType) {
      this.queryParams.operationType = operationType;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加海链财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDay(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改海链财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDay(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDay(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除海链财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)编号为"' + ids + '"的数据项？').then(function() {
        return delDay(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('account/day/export', {
        ...this.queryParams
      }, `day_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
